import memService from '../service/member'
import memberStore from '../store/member'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus';

const router = useRouter()

interface IMemberAction {
    getMember: (callback: any) => void,
    updateStatus: (id: number, status: number, callback: any) => void,
    getByMemberId: (id: number) => void,
}

export function memberHook(): IMemberAction {
    const member = memberStore()

    // 获取成员列表
    function getMember(callback: any) {
        memService.getMemberList().then((res: any) => {
            if (res.data.code == 0) {
                // console.log("响应信息", res.data)
                member.setList(res.data.data)
            } else {
                ElMessage(res.data.msg)
            }
            callback ? callback() : console.log("无回调")
        }).catch((err: any) => {
            ElMessage.error("请求失败");
        })
    }

    // 修改状态
    function updateStatus(id: number, status: number, callback: any) {
        memService.updateMemberStatus(id, status).then((res: any) => {
            // console.log(res.data)
            if (res.data.code == 0) {
                ElMessage({ type: "success", message: res.data.msg })
                getMember(callback)
            } else {
                ElMessage(res.data.msg)
            }
        }).catch((err: any) => {
            ElMessage.error("请求失败");
        })
    }

    //根据id获取单个成员信息
    function getByMemberId(id: number) {
        memService.getMmeberById(id).then((res: any) => {
            if (res.data.code == 0) {
                console.log(res.data.data)
            } else {
                ElMessage(res.data.msg)
            }
        }).catch((err: any) => {
            ElMessage.error("请求失败");
        })
    }

    return {
        getMember,
        updateStatus,
        getByMemberId,
    }
}








